{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "64792e41",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "### 1.字典的初始（{key：value}）\n",
    "\n",
    "> 1.[字典的文档]（https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "> 2.字典的重要属性\n",
    ">>1.结构：key：value（键值对）\n",
    ">>2.字典是无序的，我们通过键（key）去查找值（value），就好像list用索引（index）查找值（value）一样，只不过字典的“索引”被定义了名字\n",
    ">>3.字典对key的定义有一定的要求：key是由“不可变”的数据结构才可以定义，list是不能作为key来定义的。\n",
    ">>4.字典的key是唯一的，字典的值value也可以不唯一。（list的值是唯一的么）\n",
    "> 3.字典的创建\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a43b3ba4",
   "metadata": {},
   "outputs": [],
   "source": [
    "user = [\n",
    "    [\"jiahui\",20,\"IT\",20000],\n",
    "    [\"xiaoming\",22,\"IT\",30000]\n",
    "]\n",
    "#试对比\n",
    "user_dict = {\n",
    "    \"jiahui\":[20,\"IT\",20000],\n",
    "    \"xiaoming\":[22,\"IT\",30000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb1a218f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaoming', 22, 'IT', 30000]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询xiaoming的信息\n",
    "user[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a98d180c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[22, 'IT', 30000]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_dict[\"xiaoming\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88b862dc",
   "metadata": {},
   "source": [
    "### 2.字典的创建（常见的三种方法）\n",
    ">1.info = {key1:value1,key2:value2,...}直接按照字典格式创建\n",
    ">2.info_02 = dict(key1=value1,key2=value2,....)使用dict（）进行字典创建\n",
    ">3.运用字典增加的方式新建字典：\n",
    "info_03 = {} info_03[key1] = value1,info_03[key2] = value2 ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "12c06e0b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu10001:\"xiaoming\",\n",
    "    \"stu10002:\"lihua\",\n",
    "    \"stu10003:\"xiaohong\"\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6b8bd67a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "    stu10001=\"xiaoming\",\n",
    "    stu10002=\"lihua\",\n",
    "    stu10003=\"xiaohong\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f752dc95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {} #新建空字典\n",
    "info_03[\"stu10001\"] = \"xiaoming\"\n",
    "info_03[\"stu10002\"] = \"lihua\"\n",
    "info_03[\"stu10003\"] = \"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48234846",
   "metadata": {},
   "source": [
    "### 字典的增、删、改、查\n",
    "\n",
    ">1.字典的增加（和字典的第三种创建方法一致）：\n",
    ">>*info_03[\"stu10004\"] = \"jiahui\"\n",
    ">2.字典的查询\n",
    ">>*info_03[key_name]\n",
    ">>*注意：正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    ">>>1.key_name in info ##查看是否存在，如果存在，返回结果为ture，不存在为false\n",
    ">>>2.info_03[key_mane]\n",
    ">3.字典的修改\n",
    ">>1.查询key是否存在\n",
    ">>2.修改key的值\n",
    ">>>*例如：修改info_03中stu10003的值Alex，if“stu10003”in info_03[\"stu10003\"]=\"Alex\"\n",
    ">4.字典的删除\n",
    ">>1.pop(key)2.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1750284c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong', 'stu10004': 'jiahui'}\n"
     ]
    }
   ],
   "source": [
    "info_03[\"stu10004\"] = \"jiahui\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "469f7c12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaohong'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用key查到value的数据\n",
    "info_03[\"stu10003\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fcded5fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "查询的key不存在\n"
     ]
    }
   ],
   "source": [
    "#正确的查询字典方式\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bf854275",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'Alex', 'stu10004': 'jiahui'}\n"
     ]
    }
   ],
   "source": [
    "#修改：也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03[\"stu10003\"] = \"Alex\"\n",
    "    print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "af7a5bb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10004': 'jiahui'}\n"
     ]
    }
   ],
   "source": [
    "#字典的删除\n",
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cd8e6cf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua'}\n"
     ]
    }
   ],
   "source": [
    "#字典的删除 popitem（）\n",
    "info_03.popitem()\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce828cd1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "299.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
